package com.pda.gongpin.ui.scanquery;

import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.pda.gongpin.R;
import com.pda.gongpin.adapter.ScanQueryAdapter;
import com.pda.gongpin.bean.ScanBean;
import com.pda.gongpin.bean.ScanQueryBean;
import com.pda.gongpin.bean.ScanResultBean;
import com.pda.gongpin.bean.request.QRcodeRequest;
import com.pda.gongpin.contract.ScanQueryContract;
import com.pda.gongpin.net.NetUtils;
import com.pda.gongpin.presenter.ScanQueryPresenter;
import com.pda.gongpin.view.ScanButView;
import com.wangshen.base.app.AppActivityKey;
import com.wangshen.base.net.bean.BaseAppEntity;
import com.wangshen.base.ui.mvp.base.ui.BaseMvpActivity;
import com.wangshen.base.util.ToastUtils;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
@Route(path = AppActivityKey.SCANQUERYACTIVITY)
public class ScanQueryActivity extends BaseMvpActivity<ScanQueryPresenter> implements ScanQueryContract.View {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_back)
    TextView tvBack;
    @BindView(R.id.tv_storageBin)
    TextView tvStorageBin;
    @BindView(R.id.tv_inOriginOrderNum)
    TextView tvInOriginOrderNum;
    @BindView(R.id.tv_outOriginOrderNum)
    TextView tvOutOriginOrderNum;
    @BindView(R.id.rv_content)
    RecyclerView rvContent;
    ScanQueryAdapter scanQueryAdapter;
    @BindView(R.id.sb_scan)
    ScanButView sbScan;

    @Override
    public ScanQueryPresenter getPresenter() {
        return new ScanQueryPresenter();
    }

    @Override
    public int getView() {
        return R.layout.activity_scan_query;
    }

    @Override
    public void initView() {
        scanQueryAdapter = new ScanQueryAdapter(R.layout.item_scan_query_list, new ArrayList<>());
        rvContent.setAdapter(scanQueryAdapter);
    }

    @Override
    public void initLinsenterner() {
        sbScan.setScanCallBack(new ScanButView.ScanCallBack() {
            @Override
            public void callBack(ScanBean scanBean) {
                QRcodeRequest request = new QRcodeRequest();
                request.setQRCode(scanBean.getCode());
                getCode(request);
            }
        });
    }
    public void getCode(QRcodeRequest request) {
        NetUtils.geApi().scanTheQRCodeToVerify(request)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<BaseAppEntity<ScanResultBean>>() {
                    @Override
                    public void accept(BaseAppEntity<ScanResultBean> stringBaseAppEntity) throws Exception {
                        if (!stringBaseAppEntity.getCode().equals("200")) {
                            ToastUtils.showToast(mContext, stringBaseAppEntity.getMessage());
                            return;
                        }
                        if (stringBaseAppEntity.getData().getType().equals("A")) {
                            tvStorageBin.setText(stringBaseAppEntity.getData().getCode());
                            presenter.getStockInfoByCode(stringBaseAppEntity.getData().getCode());
                        } else if (stringBaseAppEntity.getData().getType().equals("B")){
                            tvStorageBin.setText(stringBaseAppEntity.getData().getCode());
                            presenter.getStockInfoByCode(stringBaseAppEntity.getData().getCode());
                        }else {
                            ToastUtils.showToast(mContext, "请扫正确的条形码");
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        ToastUtils.showToast(mContext, throwable.getMessage());
                    }
                });
    }
    @Override
    public void getData() {

    }

    @Override
    public void setData(ScanQueryBean scanQueryBean) {
        tvInOriginOrderNum.setText(scanQueryBean.getInOriginOrderNum());
        tvOutOriginOrderNum.setText(scanQueryBean.getOutOriginOrderNum());
        scanQueryAdapter.setNewInstance(scanQueryBean.getStockList());
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick(R.id.iv_back)
    public void onViewClicked() {
        finishActivity();
    }
}
